package cn.zb.entity.auth;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @TableName sys_user
 */
@TableName(value ="sys_user")
@Data
public class SysUser implements Serializable, UserDetails {
    @TableId(type = IdType.AUTO)
    private Long userId;

    private Long deptId;

    @JsonAlias("username")
    private String userName;

    private String nickName;

    private String email;

    private String phonenumber;

    private String sex;

    private String avatar;

    private String password;

    private String status;

    @TableLogic
    private String delFlag;

    private String loginDate;

    private static final long serialVersionUID = 1L;

    // 表示当前用户的角色与权限  属性与数据库无关
    @TableField(exist = false)
    private List<String> roles;
    @TableField(exist = false)
    private List<String> perms;

    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> list = new ArrayList<>();
        roles.forEach(role->{
            list.add(()->"ROLE_"+role);
        });
        perms.forEach(p->{
            list.add(()->p);
        });
        return list;
    }

    @Override
    public String getUsername() {
        return this.userName;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    // 是否锁定
    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return "0".equals(status);
    }
    // 密码是否过期
    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    // 账号是否可用
    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }
}